<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of pirs
 *
 * @author Albadr
 */
class pirs extends CI_Controller {

    public function index() {
        $this->load->view('index_');
    }
    
    public function testquerycompute(){
        $this->load->model('missparse');
        $this->load->model('inv_file');
        
        $docs = Array();
        $docs[0]->title = "";
        $docs[0]->content = "shipment of gold damaged in fire";
        $docs[0]->no = 3;
        $docs[1]->title = "";
        $docs[1]->content = "delivery of silver arrived in a silver truck";
        $docs[1]->no = 1;
        $docs[2]->title = "";
        $docs[2]->content = "shipment of gold arrived in a truck";
        $docs[2]->no = 2;
        
//        $query = "gold shipment";
        $query[0]->no = 1;
        $query[0]->query = "gold shipment";
        $query[1]->no = 2;
        $query[1]->query = "gold gold truck";

        $filename_of_common_words = base_url()."dc/CACM/common_words";
        $filename_of_docs = base_url()."dc/CACM/cacm.all";

        //Make dummy relevance judgement
        $qrel = array();
        $item = array(
                "no_query" => 1,
                "no_document" => array ()
        );
        $item1 = array(
                "no_query" => 1,
                "no_document" => array (1)
        );
        array_push($qrel, $item);
        array_push($qrel, $item1);

        $options=array("tf"=>"raw","norm"=>"no","idf"=>"yes","stem"=>"yes","stop"=>"yes","stopfile"=>$filename_of_common_words);
        
        $invfile = $this->inv_file->generate($docs,$options);
        $lala = $this->inv_file->computeQueryWeight($query,$options,$invfile);

        $udoc = $this->missparse->compAll($invfile,$lala,sizeof($docs));
        
        $SC = $this->missparse->compute_all_sc($invfile,$lala);
        $doexp_process = $this->missparse->calculate_performance_experiment($SC, $qrel);
        $res=array();
        for($i = 0; $i < sizeof($doexp_process); ++$i){
            $query_info = $doexp_process[$i];
            $res[$i]->no = $query_info["no_query"];
            $res[$i]->query  = $query[$query_info["no_query"]-1]->query;
            $res[$i]->niap  = $query_info["niap"];
            $doc=array();
            $result_query = $query_info["result_query"];
            for($j = 0; $j < sizeof($result_query); ++$j){
                $result_query_j = $result_query[$j];
                $doc[$j]->no = $result_query_j["no_document"];
                $doc[$j]->title = $docs[$result_query_j["no_document"]-1]->title;
            }
            $res[$i]->result=$doc;
        }
        //echo $tesprint;
        echo"<pre>===================================="; 
        print_r($udoc);
//        print_r($lala);
//        print_r($invfile);
//        print_r($res);

        echo "</pre>";
        echo memory_get_usage(true) . "\n";
    }
    
//    public function testinvfile(){
//        $this->load->model('missparse');
//        $this->load->model('inv_file');
//
//            for ($i = 0; $i < 6; $i++) {
//                echo "lala.";
//            }
//
//        $docs = Array();
//        $docs[0]->title = "";
//        $docs[0]->content = "shipment of gold damaged in fire";
//        $docs[0]->no = 3;
//        $docs[1]->title = "";
//        $docs[1]->content = "delivery of silver arrived in a silver truck";
//        $docs[1]->no = 1;
//        $docs[2]->title = "";
//        $docs[2]->content = "shipment of gold arrived in a truck";
//        $docs[2]->no = 2;
//
//        $filename_of_common_words = base_url()."dc/CACM/common_words";
//        $filename_of_docs = base_url()."dc/CACM/cacm.all";
//        $start = time();
//        $cacm = $this->missparse->docs_to_object_of_docs_clean($filename_of_docs);
//       //$koi = "She is the girl whom I love. A small part of love in my heart";
//        //$docs_processed = $this->missparse->docs_remove_common_words_array($docs,$common_words);
//        //take only 2 documents, no more
//        $r=array();
//        for($i=0;$i<=58;++$i){
//            $r[$i]=$cacm[$i];
//        }
//        $invfile = $this->inv_file->generate($r,array("tf"=>"raw","norm"=>"yes","idf"=>"yes","stem"=>"yes","stop"=>"yes","stopfile"=>$filename_of_common_words));
//
//        $elapsed = time()-$start;
//        echo "Time Elapsed for INVFILE: ".$elapsed." seconds";
//        echo memory_get_usage(true) . "\n";
//        echo"<pre>";
//        print_r($invfile);
//        //print_r($docs);
//        echo "</pre>";
//    }
//
//    public function functiontest(){
//        $start = time();
//        $filename_of_common_words = base_url()."dc/CACM/common_words";
//        $filename_of_query = base_url()."dc/CACM/query.text";
//        $filename_of_docs = base_url()."dc/CACM/cacm.all";
//
//        $this->load->model('missparse');
//
//        $query = $this->missparse->extract_objects_of_query($this->missparse->parse_query_to_objects($filename_of_query));
//        $common_words = $this->missparse->parse_common_words_to_array($filename_of_common_words);
//       //$koi = "She is the girl whom I love. A small part of love in my heart";
//        $docs = $this->missparse->extract_objects_of_doc($this->missparse->docs_to_object_of_docs($filename_of_docs));
//        $docs_processed = $this->missparse->docs_remove_common_words_array($docs,$common_words);
//        echo '<pre>';
//        print_r($docs_processed);
//        echo '</pre>';
//        echo time()-$start;
//    }
//
//    public function testCalculationPerformance(){
//        $doc_retrieved = array(1,2,3,4,5);
//        $doc_relevan = array(2,5);
//        $this->load->model('missparse');
//        $NIAP = $this->missparse->calculate_performance($doc_retrieved,$doc_relevan);
//        echo 'NIAP = ' + $NIAP;
//    }
//
    public function testDoExp(){
        $filename = "D:\My Kuliah\Informatics\Semester 7\Sistem Temu Balik Informasi\Tugas\Tugas 2\CACM\qrels.text";
        $this->load->model('missparse');
        $tes = $this->missparse->docs_to_object_of_docs_qrel($filename);
        $doexp_process = $this->missparse->calculate_performance_experiment($this->missparse->docs_to_object_of_docs_qrel($filename),$this->missparse->docs_to_object_of_docs_qrel($filename));
        echo '<pre>';
        print_r($tes);
//      print_r($doexp_process);
        echo '</pre>';
    }

}

?>
